package org.example.myleet.rosalind.prot;

import org.example.myleet.rosalind.utils.DnaCodonReader;
import org.example.myleet.rosalind.utils.RnaCodonReader;

import java.util.Map;

public class RnaTranslator {

    private final Map<String, String> rnaCodonMapAminoAcid = RnaCodonReader.getRnaCodonMapAminoAcid();

    private final Map<String, String> dnaCodonMapAminoAcid = DnaCodonReader.getDnaCodonMapAminoAcid();

    public String translateRnaToPeptide(String rna) {
        if (rna.length() < 3) {
            return "";
        }
        StringBuilder window = new StringBuilder();
        StringBuilder protein = new StringBuilder();
        int i = 0;
        for (; i < 3; ++i) {
            window.append(rna.charAt(i));
        }
        boolean started = false;
        for (; i < rna.length(); ++i) {
            String w = window.toString();
            if ("AUG".equals(w)) {
                //当检查到AUG才开始进行蛋白质翻译
                protein.append(rnaCodonMapAminoAcid.get(w));
                started = true;
            } else {
                String aminoAcid = rnaCodonMapAminoAcid.get(w);
                if ("Stop".equals(aminoAcid)) {
                    //遇到停止密码子，结束翻译
                    break;
                }
                if (started) {
                    //已经开始翻译时，连接氨基酸
                    protein.append(aminoAcid);
                }
            }
            if (started) {
                //已经开始翻译时，每次滑动前进3个核糖核酸的长度
                window.delete(0, window.length());
                window.append(rna.charAt(i++));
                window.append(rna.charAt(i++));
                window.append(rna.charAt(i));
            } else {
                //未开始翻译时，滑动1个核糖核酸的长度以进行起始密码子的匹配
                window.deleteCharAt(0);
                window.append(rna.charAt(i));
            }
        }
        return protein.toString();
    }

    public String translateDnaToPeptide(String dna) {
        if (dna.length() < 3) {
            return "";
        }
        StringBuilder window = new StringBuilder();
        StringBuilder protein = new StringBuilder();
        int i = 0;
        for (; i < 3; ++i) {
            window.append(dna.charAt(i));
        }
        boolean started = false;
        for (; i < dna.length(); ++i) {
            String w = window.toString();
            if ("ATG".equals(w)) {
                //当检查到ATG才开始进行蛋白质翻译
                protein.append(dnaCodonMapAminoAcid.get(w));
                started = true;
            } else {
                String aminoAcid = dnaCodonMapAminoAcid.get(w);
                if ("Stop".equals(aminoAcid)) {
                    //遇到停止密码子，结束翻译
                    break;
                }
                if (started) {
                    //已经开始翻译时，连接氨基酸
                    protein.append(aminoAcid);
                }
            }
            if (started) {
                //已经开始翻译时，每次滑动前进3个核糖核酸的长度
                window.delete(0, window.length());
                window.append(dna.charAt(i++));
                window.append(dna.charAt(i++));
                window.append(dna.charAt(i));
            } else {
                //未开始翻译时，滑动1个核糖核酸的长度以进行起始密码子的匹配
                window.deleteCharAt(0);
                window.append(dna.charAt(i));
            }
        }
        return protein.toString();
    }

    public static void main(String[] args) {
        RnaTranslator rnaTranslator = new RnaTranslator();
        System.out.println(rnaTranslator.translateRnaToPeptide("AUGGCCAUGGCGCCCAGAACUGAGAUCAAUAGUACCCGUAUUAACGGGUGA"));
    }
}
